package com.modifiertech.lineage.client.gui.ftbint;

import com.feed_the_beast.ftblib.lib.gui.Panel;
import com.feed_the_beast.ftblib.lib.gui.TextField;
import com.feed_the_beast.ftblib.lib.gui.Theme;
import com.modifiertech.lineage.Lineage;
import com.modifiertech.lineage.LineageRegistries;
import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.api.PlayerDataSkill;
import com.modifiertech.lineage.event.PlayerDataHandler;
import com.modifiertech.lineage.skills.SkillRegionDefine;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

public class PanelSkillV2 extends PanelWarpperScroll{
    private List<BtnSkillV2> skillButtons =new ArrayList<>(); //TODO to change to special btn later 20200404
    private ConcurrentHashMap<String,Integer> skillTempCount=new ConcurrentHashMap<>();
    private List<PanelSkillRegion> panelSkillRegion=new ArrayList<>();
    private PanelFootV2 relPanelFootV2;
    public PlayerData playerDataRelClient;
    private GuiLineageV2 mainGui;


    public PanelSkillV2(Panel panel,PanelFootV2 relPanelFootV2,GuiLineageV2 mainGui) {
        super(panel);
        this.relPanelFootV2=relPanelFootV2;
        this.mainGui=mainGui;
        initialize();
    }

    PanelSkillDescV2 getRelPanelSkillDescV2(){
        return mainGui.getPanelSkillDescV2();
    }

    @Override
    public void addWidgets() {
        //add all skill btn
        skillButtons.forEach(h->{
            add(h);
            add(h.getRelLevelAddBtn());
            add(h.getRelLevelSubBtn());
            add(h.getRelLevelTextField());
        });
        //
        panelSkillRegion.forEach(h->add(h));
        //

    }

    @Override
    public void alignWidgets() {
//        //btn
//        int pos_x=0;
//        int pos_y=0;
//        for (int i=0;i<getSkillButtons().size();i++) {
//////            if(0==i%5){
//////                pos_y+=40;
//////                pos_x=0;
//////            }else{
//                pos_x+=30;
//////            }
//            getSkillButtons().get(i).setPosAndSize(pos_x,pos_y,20,20);
//        }
//        this.setPosAndSize(0,0, ConfigHandler.guiConfigSeq[0],ConfigHandler.guiConfigSeq[1]);
        alignSkillRegion();
        alignSkillButton();
    }

    private void alignSkillButton() {
        int btn_width=20;
        int btn_height=20;
        int x_step=40;
        int y_step=30;
        for (BtnSkillV2 btnSkillV2:getSkillButtons()) {
            int levelDep=
                    btnSkillV2.skillInfo.getLevelDependency();
            PanelSkillRegion skillRegionCurrent=null;
            for (PanelSkillRegion skillRegion : panelSkillRegion) {
                if(levelDep>=skillRegion.getCurrentRegionFromLevel()
                        && levelDep<=skillRegion.getCurrentRegionToLevel()){
                    skillRegionCurrent=skillRegion;break;
                }
            }
            if(null==skillRegionCurrent)continue;
            int baseX=skillRegionCurrent.getPos_x()+25;
            int baseY=skillRegionCurrent.getPos_y()+10;
            int pox_x=
                    new BigDecimal(baseX+(btnSkillV2.skillInfo.getRowX()-1)*x_step).intValue();
            int pox_y=
                    new BigDecimal(baseY+(btnSkillV2.skillInfo.getColumnY()-1)*y_step).intValue();
            btnSkillV2.setPosAndSize(pox_x,pox_y,btn_width,btn_height);
            btnSkillV2.getRelLevelTextField().setPosAndSize(pox_x,pox_y+btn_height,btn_width,5);
            btnSkillV2.getRelLevelAddBtn().setPosAndSize(pox_x+btn_width,pox_y,5,5);
            btnSkillV2.getRelLevelSubBtn().setPosAndSize(pox_x+btn_width,pox_y+btn_height-5,5,5);
        }
    }

    public void alignSkillRegion(){
        int pos_x=0;
        int pos_y=0;
        int y_step=60;
        int y_height=55;
        for (PanelSkillRegion skillRegion : panelSkillRegion) {
            skillRegion.setPos_x(pos_x);
            skillRegion.setPos_y(pos_y);
            skillRegion.setPosAndSize(pos_x,pos_y,
                    Math.max(this.getContentWidth(),width),
                    y_height*skillRegion.getRegionSize()+
                            (skillRegion.getRegionSize()-1)*(y_step-y_height));
            pos_y+=y_step*skillRegion.getRegionSize();
            skillRegion.alignWidgets();
        }
    }

    @Override
    public void drawBackground(Theme theme, int x, int y, int w, int h) {
//        super.drawBackground(theme, x, y, w, h);
    theme.drawPanelBackground(x, y, w, h);
    }




    private void initSkillBtn(){
        //TODO mock  40 skill
//        for (int i = 0; i < 40; i++) {
//            BtnSkillV2 btnSkillV2=new BtnSkillV2(this,"Skill", GuiIconsV2.DEFAULT,relPanelSkillDescV2,relPanelFootV2,
//                    new SkillInfo());
//            getSkillButtons().add(btnSkillV2);
//        }
        for (PlayerDataSkill skill : playerDataRelClient.getAllPlayerDataSkills()) {
            BtnSkillV2 btnSkillV2=new BtnSkillV2(this,skill.getSkillName(),relPanelFootV2,skill,mainGui,new TextField(this)
            ,this);
            getSkillButtons().add(btnSkillV2);
        }

    }




    public List<BtnSkillV2> getSkillButtons() {
        return skillButtons;
    }

    public void setSkillButtons(List<BtnSkillV2> skillButtons) {
        this.skillButtons = skillButtons;
    }

    public void initialize() {
        initPlayerData();
        initSkillLevelRegion();
        initSkillBtn(); //INIT SKILL BTNS
    }

    private void initPlayerData() {
        this.playerDataRelClient = PlayerDataHandler.get(Lineage.proxy.getClientPlayer());
    }
    private void initSkillLevelRegion() {
        for (int[] ints : SkillRegionDefine.define) {
            panelSkillRegion.add(new PanelSkillRegion(this,ints[0],ints[1],ints[2],mainGui));
        }
    }

    public ConcurrentHashMap<String, Integer> getSkillTempCount() {
        return skillTempCount;
    }

    public boolean hasSkillInTemp(String skillName){
        if (!getSkillTempCount().containsKey(skillName))return false;
        if (getSkillTempCount().get(skillName)>0)return true;
        return false;
    }
//    public boolean hasAnySkillInTemp(){
//        if (getSkillTempCount().isEmpty())return false;
//        if (getSkillTempCount().values().stream().mapToInt(v->v).sum()>0)return true;
//        return false;
//    }

    public int skillInTempTotal(){
        if (getSkillTempCount().isEmpty())return 0;
        int count = 0;
        for (Map.Entry<String, Integer> stringIntegerEntry : getSkillTempCount().entrySet()) {
            Optional<PlayerDataSkill> playerDataSkill = playerDataRelClient
                    .getPlayerDataSkillByName(stringIntegerEntry.getKey());
            count+= playerDataSkill.get().getUpgradeCount()*stringIntegerEntry.getValue();
        }
        return count;
    }

    public synchronized void clearSkillTempMap(){
        getSkillTempCount().clear();
    }

}
